<?php
/*! \brief
 * This is WFDbController class
 * Manipulate all the /src/db_controller.php handle of Widget Framework
 * \author William Su
 */
class WFDbController
{
    /**
     * @var WF
     */
    protected $objFramework;
    public function __construct($objFramework){
        $this->objFramework = $objFramework;
    }
    /**
     *\brief Description
     * - This method will get all record from table WIDGET_COMPONENT_CATEGORY
     */
    public function getArrsWcomponentCategory(){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('WIDGET_COMPONENT_CATEGORY');
        $objParameter->setOrderBy('category_name ASC');
        $ret = $this->objFramework->getDataObject()->querySelectAllByParameter($objParameter);
        return $ret;
    }
    /**
	 * \brief Description 
	 * - This method will get all record from table WIDGET_COMPONENT_CATEGORY with customized fields information
	 */
	public function getArrsWcomponentCategoryWithCustomizedFields() {
		// get all category
		$objParameter = $this->objFramework->getParameter();
		$objParameter->appendSelect( 'wc.category_product_id,wc.category_name,wc.category_proxy,s.customFields ' );
		$objParameter->appendFrom( 'WIDGET_COMPONENT_CATEGORY wc left join serverlisttype s on wc.category_proxy = s.type' );
		$objParameter->setOrderBy( 'wc.category_name ASC' );
		$arrCategory = $this->objFramework->getDataObject()->querySelectAllByParameter( $objParameter );
		
		return $arrCategory;
	}	
    /**
	 * \brief Description
	 * - This method will get customfields by product_id
	 */
	public function getCustomizedFieldsByProductId($productId) {
		$objParameter = $this->objFramework->getParameter();
		$objParameter->appendSelect( 's.customFields' );
		$objParameter->appendFrom( 'WIDGET_COMPONENT_CATEGORY wc join serverlisttype s on wc.category_proxy = s.type' );
		$objParameter->andWhereEqual( 'wc.category_product_id', $productId );
		$customFields = $this->objFramework->getDataObject()->querySelectAllByParameter( $objParameter );
		return $customFields;
	}	
	/**
	 * \brief Description
     * - This method will get proxy module from table WIDGET_COMPONENT_CATEGORY by specific proxy module
     */
    public function getArrayProxyModuleByProductID($wcu_product_id){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('WIDGET_COMPONENT_CATEGORY');
		$objParameter->andWhereEqual('category_product_id', $wcu_product_id);
        $ret = $this->objFramework->getDataObject()->querySelectAllByParameter($objParameter);
        return $ret;
    }	
    /**
	 * \brief Description
     * - This method will get record from table CONTAINER by container_id
     */
    public function getAssocContainerByContainerId($intContainerId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('CONTAINER');
        $objParameter->andWhereEqual('container_id', $intContainerId);
        $ret = $this->objFramework->getDataObject()->querySelectRowByParameter($objParameter);
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get record from table CONTAINER by container_id
     */
    public function getAssocPageInfoByCondition($strPageName,$intUserId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendSelect('pdata');
        $objParameter->appendFrom('users');
        $objParameter->andWhereEqual('id', $intUserId);
        $jsonPdata = $this->objFramework->getDataObject()->querySelectOneByParameter($objParameter);
        $arrsPdata = json_decode($jsonPdata,true);
        $intContainerId = '1'; //default
        $strContainerTypeName = 'dashBoard'; //default
        $strContainerName = 'dashBoard'; //default
        $intPageNumber = '1'; //default
        if(is_array($arrsPdata) && $arrsPdata){
            foreach($arrsPdata as $arrPdata){
                if(isset($arrPdata['pageName'])){
                    $arrPdata['pageName'] = trim($arrPdata['pageName']);
                    if($arrPdata['pageName'] == $strPageName){
                        $intContainerId = $arrPdata['container_id'];
                        $intPageNumber = $arrPdata['pageNumber'];
                        break;
                    }
                }
            }
        }
        
        $assocContainer = $this->getAssocContainerByContainerId($intContainerId);
        if(isset($assocContainer['container_type_name']) && $assocContainer['container_type_name']){
            $strContainerTypeName = $assocContainer['container_type_name'];
        }
        if(isset($assocContainer['container_name']) && $assocContainer['container_name']){
            $strContainerName = $assocContainer['container_name'];
        }
		$ret = array (
				'container_id' => $intContainerId,
				'page_number' => $intPageNumber,
				'container_name' => $strContainerName,
				'container_type_name' => $strContainerTypeName 
        );
        return $ret;
    }
    /**
	 * \brief Description
     * - This method will get container_id from table CONTAINER by container_name
     */
    public function getContainerIdByContainerName($strContainerName){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendSelect('container_id');
        $objParameter->appendFrom('CONTAINER');
        $objParameter->andWhereEqual('container_name', $strContainerName);
        $ret = $this->objFramework->getDataObject()->querySelectOneByParameter($objParameter);
        return $ret;
    }
    /**
	 * \brief Description
     * - This method will get wcomponent_param from table WIDGET_COMPONENT by wcomponent_name
     */
	public function getWcomponentParamByWcomponentName($strWcomponentName) {
		$objParameter = $this->objFramework->getParameter ();
		$objParameter->appendSelect ( 'wcomponent_param' );
		$objParameter->appendFrom ( 'WIDGET_COMPONENT' );
		$objParameter->andWhereEqual ( 'wcomponent_name', $strWcomponentName );
		$ret = $this->objFramework->getDataObject ()->querySelectOneByParameter ( $objParameter );
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get arrsWcomponent from table WIDGET_COMPONENT by category_product_id
     */
    public function getArrsWcomponentByCategoryProductId($intCategoryProductId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendSelect('wcomponent_id,wcomponent_name,wcomponent_version,wcomponent_param','wc');
        $objParameter->appendFrom('WIDGET_COMPONENT','wc');
        $objParameter->appendFrom('WIDGET_COMPONENT_CATEGORY','wc_c');
        $objParameter->appendFrom('WIDGET_COMPONENT_CATEGORY_MAPPING','wc_cm');
        $objParameter->andWhereEqual('wc.wcomponent_id=wc_cm.wcomponent_id');
        $objParameter->andWhereEqual('wc_c.category_id=wc_cm.category_id');
        $objParameter->andWhereEqual('wc_c.category_product_id', $intCategoryProductId);
        $objParameter->andWhereEqual('wc.wcomponent_status', 1);
        $ret = $this->objFramework->getDataObject()->querySelectAllByParameter($objParameter);
        return $ret;
    }
    /**
     *\brief Description
     * - This method will insert table CONTAINER by jsonDataContainer if record is not existed in DB
     * - And update table CONTAINER by jsonDataContainer if container_id is existed in DB
     */
    public function insertOrUpdateContainerByJsonData($jsonDataContainer){
        $arrsDataContainer = $this->getArrsContainerByJsonContainer($jsonDataContainer);
        if(is_array($arrsDataContainer) && $arrsDataContainer){
            foreach($arrsDataContainer as $assocDataContainer){
                $this->insertOrUpdateContainerByAssocData($assocDataContainer);
            }
        }
    }
    /**
     *\brief Description
     * - This method will insert table CONTAINER by assocDataContainer if record is not existed in DB
     * - And update table CONTAINER by assocDataContainer if container_id is existed in DB
     */
    public function insertOrUpdateContainerByAssocData($assocDataContainer){
        $isContainerExisted = false;
        if(isset($assocDataContainer['container_id']) && $assocDataContainer['container_id']){
            $intContainerId = $assocDataContainer['container_id'];
            $isContainerExisted = $this->isContainerExistedByContainerId($intContainerId);
        }
        if(false === $isContainerExisted){
            $this->insertContainerByAssocData($assocDataContainer);
        }else{
            $this->updateContainerByAssocData($intContainerId, $assocDataContainer);
        }
    }
    /**
     *\brief Description
     * - This method will insert table CONTAINER by assocDataContainer
     */
    protected function insertContainerByAssocData($assocDataContainer){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('CONTAINER');
        $objParameter->setAssocData($assocDataContainer);
        $ret = $this->objFramework->getDataObject()->queryInsertByParameter($objParameter);
        return $ret;
    }
    /**
     *\brief Description
     * - This method will update table CONTAINER by assocDataContainer by container_id
     */
    protected function updateContainerByAssocData($intContainerId, $assocDataContainer){
        if(! $intContainerId){
            $strErrMsg = __CLASS__ . '::' . __FUNCTION__ . '()=>$intContainerId is required!';
            throw new WFControllerDbControllerException($strErrMsg);
        }
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('CONTAINER');
        $objParameter->setAssocData($assocDataContainer);
        $objParameter->andWhereEqual('container_id', $intContainerId);
        $ret = $this->objFramework->getDataObject()->queryUpdateByParameter($objParameter);
        return $ret;
    }
    /**
     *\brief Description
     * - This method will check is container record is existed in  table CONTAINER by container_id
     */
    public function isContainerExistedByContainerId($intContainerId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('CONTAINER');
        $objParameter->andWhereEqual('container_id', $intContainerId);
        $ret = $this->objFramework->getDataObject()->querySelectCountByParameter($objParameter);
        return $ret ? true : false;
    }
    /**
     *\brief Description
     * - This method will get arrsContainer by jsonContainer
     */
    protected function getArrsContainerByJsonContainer($jsonContainer){
        $arrsContainer = json_decode($jsonContainer, true);
        $ret = array();
        if(is_array($arrsContainer) && $arrsContainer){
            foreach($arrsContainer as $arrContainer){
                if(isset($arrContainer['container_param']) && is_array($arrContainer['container_param'])){
                    $arrContainer['container_param'] = json_encode($arrContainer['container_param']);
                }
                $ret[] = $arrContainer;
            }
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get Get arrsPid from table.users pdata by condition
     */
    protected function getArrsPidFromUsersPdataByCondition($intContainerId, $intUserId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendSelect('pdata');
        $objParameter->appendFrom('users');
        $objParameter->andWhereEqual('id', $intUserId);
        $jsonUserPdata = $this->objFramework->getDataObject()->querySelectOneByParameter($objParameter);
        $arrsUserPdata = json_decode($jsonUserPdata, true);
        $ret = array();
        if(is_array($arrsUserPdata) && $arrsUserPdata){
            foreach($arrsUserPdata as $arrUserPdata){
                if($arrUserPdata['container_id'] == $intContainerId){
                    $ret[] = $arrUserPdata['pageNumber'];
                }
            }
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get Get arrsUserdata from table.userdata by condition
     */
    public function getArrsUserdataByCondition($intContainerId, $intUserId){
        $arrsPid = $this->getArrsPidFromUsersPdataByCondition($intContainerId, $intUserId);
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendFrom('userdata');
        $objParameter->andWhereIn('pid', $arrsPid);
        $objParameter->andWhereEqual('uid', $intUserId);
        $arrsUserdata = $this->objFramework->getDataObject()->querySelectAllByParameter($objParameter);
        return $arrsUserdata;
    }
    /**
     *\brief Description
     * - This method will get Get arrsPdata by container_id and jsonPdata
     */
    public function getArrsPdataByContainerIdAndJsonPdata($intContainerId,$jsonPdata){
        $arrsPdata = json_decode($jsonPdata, true);
        $ret = array();
        if(is_array($arrsPdata) && $arrsPdata){
            foreach($arrsPdata as $arrPdata){
                if($intContainerId == $arrPdata['container_id']){
                    $ret[] = $arrPdata;
                }
            }
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get Get arrsPdata from table.users by container_id and user_id
     */
    public function getArrsPdataByContainerIdAndUserId($intContainerId,$intUserId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendSelect('pdata');
        $objParameter->appendFrom('users');
        $objParameter->andWhereEqual('id', $intUserId);
        $jsonPdata = $this->objFramework->getDataObject()->querySelectOneByParameter($objParameter);
        $arrsPdata = json_decode($jsonPdata, true);
        $ret = array();
        if(is_array($arrsPdata) && $arrsPdata){
            foreach($arrsPdata as $arrPdata){
                if($intContainerId == $arrPdata['container_id']){
                    $ret[] = $arrPdata;
                }
            }
        }
        return $ret;
    }
    /**
     *\brief Description
     * - This method will get Get arrsPdata from table.users by user_id
     */
    public function getArrsPdataByUserId($intUserId){
        $objParameter = $this->objFramework->getParameter();
        $objParameter->appendSelect('pdata');
        $objParameter->appendFrom('users');
        $objParameter->andWhereEqual('id', $intUserId);
        $jsonPdata = $this->objFramework->getDataObject()->querySelectOneByParameter($objParameter);
        $arrsPdata = json_decode($jsonPdata, true);
        return $arrsPdata;
    }
    /**
     *\brief Description
     * - This method will get Get PageNumber+1 from table.users by user_id
     */
    public function getPageNumberPlusOneByUserId($intUserId){
        $arrsPdata = $this->getArrsPdataByUserId($intUserId);
        $intMaxPageNumber = 0;
        if(is_array($arrsPdata) && $arrsPdata){
            foreach($arrsPdata as $arrPdata){
                if($intMaxPageNumber < $arrPdata['pageNumber']){
                    $intMaxPageNumber = $arrPdata['pageNumber'];
                }
            }
        }
        return $intMaxPageNumber+1;
    }
}
